#include <iostream>
#include "cpp-httplib/httplib.h"
#include "searcher.hpp"

const std::string src_path = "data/output/raw.bin";
const std::string root_path = "./wwwroot";

int main()
{
    Searcher* searcher = new Searcher();
    searcher->InitSearcher(src_path);

    // 创建HTTP对象
    httplib::Server svr;

    // 设置web根目录
    svr.set_base_dir(root_path);

    // 注册GET方法
    svr.Get("/hi", [](const httplib::Request& req, httplib::Response& rsp) {
        rsp.set_content("hello world，我是HTTP!", "text/plain: charset=utf-8");
        });

    svr.Get("/s", [&searcher](const httplib::Request& req, httplib::Response& rsp)
        {
            if (!req.has_param("word")) // 是否有参数
                rsp.set_content("输入有误，请重新输入!", "text/plain: charset=utf-8");
            else
            {   // 提取参数
                std::string word = req.get_param_value("word");
                std::cout << "user query : " << word << std::endl;
                std::string json_string;
                // 进行搜索
                searcher->Search(word, &json_string);
                // 返回结果
                rsp.set_content(json_string, "applicaton/json");
            }
        });
    // 设置监听
    svr.listen("0.0.0.0", 8080);

    delete searcher;

    return 0;
}
